package com.example.a33mvvm.register;

import androidx.lifecycle.Observer;

import android.content.Intent;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.example.a33mvvm.R;
import com.example.a33mvvm.databinding.ActivityRegisterBinding;
import com.example.a33mvvm.login.LoginActivity;
import com.example.base_mvvm.base.view.BaseView;
import com.example.base_mvvm.entity.RegisterEntity;

public class RegisterActivity extends BaseView<ActivityRegisterBinding,RegisterViewModel> {


    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<RegisterEntity>() {
            @Override
            public void onChanged(RegisterEntity registerEntity) {
                if(registerEntity.getCode() == 200){
                    ToastUtils.showLong("注册成功");
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                }else {
                    ToastUtils.showLong(registerEntity.getMessage());
                }
            }
        });
        viewModel.failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.registerRegister.setOnClickListener(view -> {
           String username = dataBinding.registerUsername.getText().toString().trim();
           String password = dataBinding.registerPassword.getText().toString().trim();
           String re_password = dataBinding.registerRePassword.getText().toString().trim();
           if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)||TextUtils.isEmpty(re_password)){
               ToastUtils.showLong("请输入账号密码");
           }else {
               viewModel.register(username, password, re_password);
           }
        });
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}